<?php
/*
 * Created on 07.04.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class UsersController extends AppController{
	function beforeFilter(){
		//$this->checkSession();
		/* if ($this->Session->check('User')) {
		$this->set('access', true);
		$this->set('user', $this->Session->write('User'));
		}*/
		$this->sessionUser = $this->Session->read('User');
	}

	function login() {
		//Don't show the error message if no data has been submitted.
		$this->set('error', false);
		$this->set('hidden', false);

		if ($this->Session->check('User')) {
			$this->redirect('/users/home');
		};
		// If a user has submitted form data:
		if (!empty($this->data)){
			// First, let's see if there are any users in the database
			// with the username supplied by the user using the form:

			$someone = $this->User->findByUsername($this->data['User']['username']);

			// At this point, $someone is full of user data, or its empty.
			// Let's compare the form-submitted password with the one in
			// the database.

			if(!empty($someone['User']['password']) && $someone['User']['hidden']==false && $someone['User']['password'] == $this->data['User']['password']){
				// Note: hopefully your password in the DB is hashed,
				// so your comparison might look more like:
				// md5($this->data['User']['password']) == ...

				// This means they were the same. We can now build some basic
				// session information to remember this user as 'logged-in'.

				$this->Session->write('User', $someone['User']);

				// Now that we have them stored in a session, forward them on
				// to a landing page for the application.

				$this->redirect('/users/home');
                
			}
			// Else, they supplied incorrect data:
			else if ($someone['User']['hidden']==true){
				$this->set('hidden', true);
			} else {
				// Remember the $error var in the view? Let's set that to true:
				$this->set('error', true);
			}
		}
	}

	function logout(){
		// Redirect users to this action if they click on a Logout button.
		// All we need to do here is trash the session information:

		$this->Session->delete('User');

		// And we should probably forward them somewhere, too...
     
		$this->redirect('/');
	}

	function home(){
		$this->checkSession();
		// Redirect users to this action if they click on a Logout button.
		// All we need to do here is trash the session information:

		// And we should probably forward them somewhere, too...
     
		//$this->redirect('/users/home');
	}

	function edit(){
		$this->checkSession();

		if (!empty($this->data)){
			$user = $this->data;
			foreach($user['User'] as $key => $value) {
				$userForDb[$key] = "'".$value."'";
			}
			if ($this->User->updateAll($userForDb,'id = '.$userForDb['id'])) {
				//$this->flash('Your post has been saved.','/posts');
				$this->set('saved', true);
				//$this->redirect('/users/edit');
			}
		} else {
			$user = $this->User->findById($this->sessionUser['id']);
			$this->set('saved', false);
		}
		$this->set('user', $user['User']);
	}

	function save(){
		
	}

	function register() {
		if ($this->Session->check('User')){
			$this->redirect('/users/edit');
		}

		if (!empty($this->data)){
			$this->data['User']['hidden'] = 1;
			$this->data['User']['group'] = 0;
			if ($this->User->save($this->data)){
				$player = $this->User->findPlayerByUsername($this->data['User']['username']);
				$this->set('confirm', true);
				$this->set('player', $player);
			}
		} else {
			$this->set('confirm', false);
		}
	}

	function confirm(){
		
	}
}

?>
